﻿#include "souistd.h"
#include "res.mgr/SStylePool.h"
#include <map>

namespace SOUI {


    //////////////////////////////////////////////////////////////////////////
    // SStylePoolMgr
    pugi::xml_node SStylePoolMgr::GetStyle( LPCWSTR lpszName) {
        return m_stylePool.GetStyle(lpszName);
    }
    
    
    BOOL SStylePoolMgr::Init(pugi::xml_node xmlNode) {
        return m_stylePool.Init(xmlNode);
    }
    
    void SStylePoolMgr::Reset() {
    
    }
    
    SStylePoolMgr::~SStylePoolMgr() {
        Reset();
    }
    
    //////////////////////////////////////////////////////////////////////////
    // STemplatePoolMgr
    SStringW STemplatePoolMgr::GetTemplateString(const SStringW &strName) {
        return m_templatePool.GetTemplateString(strName);
    }
    
    
    BOOL STemplatePoolMgr::Init(pugi::xml_node xmlNode) {
        return m_templatePool.Init(xmlNode);
    }
    
    void STemplatePoolMgr::Reset() {
    
    }
    
    STemplatePoolMgr::~STemplatePoolMgr() {
        Reset();
    }
    
    
    
    ///////////////////////////////
    
    
    class XStylePoolImpl {
      public:
        ~XStylePoolImpl() {
            m_xmlDatas.clear();
        }
      public:
        /**
           * GetStyle
           * @brief    Get style object from pool by class name
           * @param    LPCWSTR lpszName --  name of style
           * @param [out]  SwndStyle & style --  style
           * @return   BOOL -- TRUE: success; FALSE: not exist
           * Describe
           */
        pugi::xml_node GetStyle(LPCWSTR lpszName) {
            auto iterFind = m_xmlDatas.find(lpszName);
            if (iterFind != m_xmlDatas.end()) {
                return iterFind->second;
            }
            return pugi::xml_node();
        }
        
        /**
         * Init
         * @brief    Load Style table from xml node
         * @param    pugi::xml_node xmlNode --  xml node that describe style list
         * @return   BOOL -- TRUE: loaded; FALSE:failed;
         * Describe
         */
        BOOL Init(pugi::xml_node xmlStyleRoot) {
        
            if (!xmlStyleRoot) {
                return FALSE;
            }
            
            xmlStyleRoot = m_xmlDoc.append_copy(xmlStyleRoot);
            for (pugi::xml_node xmlChild = xmlStyleRoot.first_child(); xmlChild; xmlChild = xmlChild.next_sibling()) {
                SStringW strClsName = xmlChild.name();
                if (strClsName.CompareNoCase(L"class") == 0) {
                    strClsName = xmlChild.attribute(L"name").value();
                    if (strClsName.IsEmpty()) {
                        continue;
                    }
                    xmlChild.remove_attribute(L"name");//删除name属性，防止该属性被处理
                }
                m_xmlDatas[strClsName] = xmlChild;
            }
            
            return TRUE;
        }
        
      private:
        pugi::xml_document  m_xmlDoc;
        std::map<SOUI::SStringW, pugi::xml_node> m_xmlDatas;
        
    };
    
    
    /// <summary>
    ///
    /// </summary>
    XStylePool::XStylePool() {
        m_pImpl = new XStylePoolImpl();
    }
    XStylePool::~XStylePool() {
        if (m_pImpl) {
            delete m_pImpl;
            m_pImpl = nullptr;
        }
    }
    
    pugi::xml_node XStylePool::GetStyle(LPCWSTR lpszName) {
        if (m_pImpl) {
            return m_pImpl->GetStyle(lpszName);
        }
        return pugi::xml_node();
    }
    
    int XStylePool::Init(pugi::xml_node xmlNode) {
        if (m_pImpl) {
            return m_pImpl->Init(xmlNode);
        }
        return FALSE;
    }
    
    ///
    class XTemplatePoolImpl {
      public:
        ~XTemplatePoolImpl() {
            m_xmlDatas.clear();
        }
      public:
        BOOL Init(pugi::xml_node xmlNode) {
        
            if (!xmlNode) {
                return FALSE;
            }
            
            for (pugi::xml_node xmlChild = xmlNode.first_child(); xmlChild; xmlChild = xmlChild.next_sibling()) {
                SStringW strTempName = xmlChild.name();
                pugi::xml_writer_buff writer;
                xmlChild.first_child().print(writer, L"", pugi::format_default, pugi::encoding_utf16);
                SStringW strValue = SStringW(writer.buffer(), writer.size());
                m_xmlDatas[strTempName] = strValue;
            }
            return TRUE;
        }
        SStringW GetTemplateString(const SStringW &strName) const {
            auto iter = m_xmlDatas.find(strName);
            return iter == m_xmlDatas.end() ? L"" : iter->second;
        }
        
      private:
        std::map<SOUI::SStringW, SStringW> m_xmlDatas;
    };
    
    /// <summary>
    /// </summary>
    XTemplatePool::XTemplatePool() {
        m_pImpl = new XTemplatePoolImpl();
    }
    XTemplatePool::~XTemplatePool() {
        if (m_pImpl) {
            delete m_pImpl;
            m_pImpl = nullptr;
        }
    }
    BOOL XTemplatePool::Init(pugi::xml_node xmlNode) {
        return 0;
    }
    SStringW XTemplatePool::GetTemplateString(const SStringW &strName) const {
        return SStringW();
    }
}//end of namespace SOUI
